package com.burgeon.backend.Controller;

import com.burgeon.backend.mappers.AccountMapper;
import com.burgeon.backend.stsutil.ResponseResult;
import com.burgeon.backend.entity.Wechat;
import com.burgeon.backend.service.WechatService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.Map;
@CrossOrigin
@RestController
@RequestMapping("wechat")
public class WXController extends BaseController {

    @Autowired
    WechatService wechatService;
    @Autowired
    AccountMapper accountMapper;

    @RequestMapping("getwechat")
    public ResponseResult<Wechat> GetWechat(String account) {
        //String account = session.getAttribute("account").toString();
        Wechat data = wechatService.GetWechatByAccount(account);
        return new ResponseResult<Wechat>(SUCCESS,data);
    }

    @RequestMapping("updatawechat")
    public ResponseResult<Void> UpDataWechat(Wechat wechat, String account) {
        //String account = session.getAttribute("account").toString();
        String url = "http://lfwd.cloudsmicro.com";
        wechat.setRedirectUrl(url);
        wechat.setAccount(account);
        wechat.setToken(accountMapper.GetTokenForAccount(account));
        wechatService.UpDataWechat(wechat);
        return new ResponseResult<>(SUCCESS);
    }

    @RequestMapping("getopenid")
    public ResponseResult<String> getOpenid(String code,String token) {

        String data = wechatService.H5Opneid(code, token);
        /*Map data = wechatService.GetSDKSign(uri, token);
        System.err.println(data);*/
        return new ResponseResult<String>(SUCCESS,data);
    }

    @RequestMapping("judge")
    public ResponseResult<Void> WhereToGo(String code, String uri, HttpSession session) {
        String token = session.getAttribute("token").toString();

        wechatService.Judge(code, token, session);
        /*Map data = wechatService.GetSDKSign(uri, token);
        System.err.println(data);*/
        return new ResponseResult<>(SUCCESS);
    }
    //注册或认证
    @RequestMapping("roc")
    public ResponseResult<Void> ROC(HttpSession session) {
        String token = session.getAttribute("token").toString();
        String phone = session.getAttribute("phone").toString();
        wechatService.JudgeByPhone(token,phone, session);
        /*Map data = wechatService.GetSDKSign(uri, token);
        System.err.println(data);*/
        return new ResponseResult<>(SUCCESS);
    }

    @RequestMapping("weizhi")
    public ResponseResult<Map> Site(String uri,HttpSession session){
        String token = session.getAttribute("token").toString();
        Map data = wechatService.GetSDKSign(uri, token);
        return new ResponseResult<Map>(SUCCESS, data);
    }

    @RequestMapping("rus")
    public ResponseResult<Wechat> GetData(String token, HttpSession session) {
        Wechat data = wechatService.getInfo(token);
        session.setAttribute("token", token);
        return new ResponseResult<Wechat>(SUCCESS, data);
    }

    @RequestMapping("sout")
    public void getSout(String data){
        System.err.println(data);
    }

}
